package vendas.dao;

import gminet.infra.dao.DataAccessObject;
import gminet.infra.db.TransactionException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import vendas.dto.ActiveStatus;
import vendas.dto.Product;


public class ProductDAO extends DataAccessObject {

    public static Product findByPK(int productId, Connection conn) throws TransactionException {

        StringBuffer sbSql = new StringBuffer();
        sbSql.append("SELECT A.CD_PRODUCT      CD_PRODUCT     ");
        sbSql.append("     , A.TX_NAME         TX_NAME        ");
        sbSql.append("     , A.TX_DESCRIPTION  TX_DESCRIPTION ");
        sbSql.append("     , A.TX_IMAGE_PATH   TX_IMAGE_PATH  ");
        sbSql.append("     , A.ID_ACTIVE       ID_ACTIVE      ");
        sbSql.append("  FROM TB_PRODUCT     A ");
        sbSql.append(" WHERE A.CD_PRODUCT = ? ");

        Product product = null;
        try {
            PreparedStatement ps = conn.prepareStatement(sbSql.toString());
            setInt(ps, 1, productId);
            ResultSet rs = ps.executeQuery();
            if (rs.next()) {
                product = fromResultSet(rs);
            }
            close(ps);
        } catch (SQLException sqle) {
            throw new TransactionException("ProductDAO.findByPK (" + sqle.getMessage() + ")");
        }
        return product;
    }

    public static List<Product> findAll(Connection conn) throws TransactionException {

        StringBuffer sbSql = new StringBuffer();
        sbSql.append("SELECT A.CD_PRODUCT      CD_PRODUCT     ");
        sbSql.append("     , A.TX_NAME         TX_NAME        ");
        sbSql.append("     , A.TX_DESCRIPTION  TX_DESCRIPTION ");
        sbSql.append("     , A.TX_IMAGE_PATH   TX_IMAGE_PATH  ");
        sbSql.append("     , A.ID_ACTIVE       ID_ACTIVE      ");
        sbSql.append("  FROM TB_PRODUCT A ");
        sbSql.append(" ORDER BY A.TX_NAME ");
        List<Product> productsList = new ArrayList<Product>(100);
        try {
            PreparedStatement ps = conn.prepareStatement(sbSql.toString());
            ResultSet rs = ps.executeQuery();
            while (rs.next()) {
                Product product = fromResultSet(rs);
                productsList.add(product);
            }
            close(rs);
        } catch (SQLException sqle) {
            throw new TransactionException("ProductDAO.findAll (" + sqle.getMessage() + ")");
        }
        return productsList;
    }

    public static void insert(Product product, Connection conn) throws TransactionException {

        StringBuffer sbSql = new StringBuffer();
        sbSql.append("INSERT INTO TB_PRODUCT ");
        sbSql.append("     ( CD_PRODUCT      ");
        sbSql.append("     , TX_NAME         ");
        sbSql.append("     , TX_DESCRIPTION  ");
        sbSql.append("     , TX_IMAGE_PATH   ");
        sbSql.append("     , ID_ACTIVE       ");
        sbSql.append(") VALUES (?,?,?,?,?)");
        try {
            PreparedStatement ps = conn.prepareStatement(sbSql.toString());
            int pos = 1;
            setInt(ps, pos++, product.getId());
            setString(ps, pos++, product.getName());
            setString(ps, pos++, product.getDescription());
            setString(ps, pos++, product.getImageFileName());
            setInt(ps, pos++, product.getActiveStatus().getId());
            ps.executeUpdate();
            close(ps);
        } catch (SQLException sqle) {
            throw new TransactionException("ProductDAO.insert (" + sqle.getMessage() + ")");
        }
    }

    public static void update(Product product, Connection conn) throws TransactionException {

        StringBuffer sbSql = new StringBuffer();
        sbSql.append("UPDATE TB_PRODUCT         ");
        sbSql.append("   SET TX_NAME        = ? ");
        sbSql.append("     , TX_DESCRIPTION = ? ");
        sbSql.append("     , TX_IMAGE_PATH  = ? ");
        sbSql.append("     , ID_ACTIVE      = ? ");
        sbSql.append(" WHERE CD_PRODUCT     = ? ");
        try {
            PreparedStatement ps = conn.prepareStatement(sbSql.toString());
            int pos = 1;
            setString(ps, pos++, product.getName());
            setString(ps, pos++, product.getDescription());
            setString(ps, pos++, product.getImageFileName());
            setInt(ps, pos++, product.getActiveStatus().getId());
            setInt(ps, pos++, product.getId());
            ps.executeUpdate();
            close(ps);
        } catch (SQLException sqle) {
            throw new TransactionException("ProductDAO.update (" + sqle.getMessage() + ")");
        }
    }

    public static void delete(Product product, Connection conn) throws TransactionException {

        StringBuffer sbSql = new StringBuffer();
        sbSql.append("DELETE FROM TB_PRODUCT ");
        sbSql.append(" WHERE CD_PRODUCT = ?  ");
        try {
            PreparedStatement ps = conn.prepareStatement(sbSql.toString());
            setInt(ps, 1, product.getId());
            ps.executeUpdate();
            close(ps);
        } catch (SQLException sqle) {
            throw new TransactionException("ProductDAO.delete (" + sqle.getMessage() + ")");
        }
    }

    public static int getNextCodigo(Connection conn, int iRange, int eRange) throws TransactionException {

        try {
            StringBuffer sbSql = new StringBuffer();
            sbSql.append("SELECT MAX(CD_PRODUCT) AS CD_MAX ");
            sbSql.append("  FROM TB_PRODUCT     ");
            sbSql.append(" WHERE CD_PRODUCT > ? ");
            sbSql.append("   AND CD_PRODUCT < ? ");

            PreparedStatement ps = conn.prepareStatement(sbSql.toString());
            setInt(ps, 1, iRange);
            setInt(ps, 2, eRange);
            ResultSet rs = ps.executeQuery();
            int max = 0;
            if (rs.next()) {
                max = rs.getInt("CD_MAX");
            }
            close(rs);
            return (max != 0 ? max : iRange) + 1;
        } catch (SQLException sqlEx) {
            throw new TransactionException(sqlEx);
        }
    }

    private static Product fromResultSet(ResultSet rs) throws SQLException {

        // TB_PRODUCT
        Product product = new Product();
        product.setId(getInt(rs, "CD_PRODUCT"));
        product.setName(getString(rs, "TX_NAME"));
        product.setDescription(getString(rs, "TX_DESCRIPTION"));
        product.setImageFileName(getString(rs, "TX_IMAGE_PATH"));
        product.setActiveStatus(ActiveStatus.getActiveStatusById(getByte(rs, "ID_ACTIVE")));
        //
        return product;
    }

}
